package com.cb.controller.front.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.ui.AbstractProcessingFilter;
import org.springframework.security.ui.savedrequest.SavedRequest;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.controller.BaseListController;

/**
 * 登录后的处理
 * @author sdm
 *
 */
public class LoginController extends BaseListController {

	@Override
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String url=getTargetUrl(request);		
		
		return new ModelAndView("login","url",url);
	}
	
	
	private String getTargetUrl(HttpServletRequest req) {
		
		String targetUrl = null;
		
		HttpSession session=req.getSession(false);
		// retrieve saved requested url (saved by Acegi)
		if (session != null) {
			SavedRequest sq = (SavedRequest) session.getAttribute(AbstractProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY);
			if (sq != null) {
				targetUrl = sq.getFullRequestUrl();
				session.removeAttribute(AbstractProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY);
			}
		}
		// if no requested url, set to use default url
		if (targetUrl == null || "".equals(targetUrl)) {
			targetUrl =req.getContextPath()+"/member/index.do";
		}
		return targetUrl;
	}
}
